#include "template.h"
#include "parameterlist.h"
#include "parameter.h"
#include "body.h"
#include "errorlog.h"
#include "component.h"
#include "memberlist.h"

Template::Template(const QString& name, ParameterList* params, Body* body, const YYLTYPE& loc)
        : Namespace( loc ), m_name(name), m_params(params), m_body(body)
{
    appendChild( params );
    appendChild( body );
}

Node* Template::getEntry( const QString& name )
{
    Node* n = m_params->parameter(name);
    if ( n )
        return n;
    return getLiftedEntry(name);
}

void Template::resolveAll()
{
    this->liftNames();
    this->Namespace::resolveAll();    

    foreach( Parameter* p, parameterList()->parameters() )
    {
        if ( !p->type().isValid() )
            ErrorLog::error( p->location(), "Template parameters must be typed");
    }

    if ( this->m_name == "main" && this->m_params->count() != 0 )
        ErrorLog::error( location(), "The main template must not have any parameters");
}

Component* Template::component()
{
    MemberList* lst = dynamic_cast<MemberList*>(this->parentNode());
    if ( lst )
        return dynamic_cast<Component*>(lst->parentNode());
    return 0;
}
